time and localization

Time

  • the time is determined by four parts:
    • time value
    • time standard (localtime, UTC)
    • time zone (Asia/Tehran)
    • Daylight Saving Time (DST)
  • Hardware clock - Real Time Clock (RTC) - CMOS clock [N01]
    • Only have the ability to store time: year, month, day, hour, minute, and second
    • No timezone, DST or standard
In [6]:
timedatectl
      Local time: Wed 2017-03-22 16:20:13 IRDT
  Universal time: Wed 2017-03-22 11:50:13 UTC
        RTC time: Wed 2017-03-22 11:50:12
       Time zone: Asia/Tehran (IRDT, +0430)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

Standards

In [2]:
sudo hwclock --show --localtime # do not adjust to local timezone. (it's already localtime)

It will display what the current time is according to the hardware clock.
If this time matches whatever your watch says, then the hardware clock is set to local time.
If the output from hwclock is not local time, chances are it is set to UTC time.

systemd-timedated reads /etc/adjtime, and depending on the contents of the file, it sets the clock to either UTC or local time.
If /etc/adjtime isn't present at boot, systemd-timedated will assume that hardware clock is set to UTC.

When a os uses UTC it will consider CMOS time as a UTC then makes an adjustment to it.

when using localtime:

In [ ]:
sudo timedatectl set-local-rtc true

cat /etc/adjtime # used by hwclock

In [ ]:
sudo timedatectl set-local-rtc false # do not use localtime -> Set to UTC
In [5]:
cat /etc/adjtime
cat: /etc/adjtime: No such file or directory

Why UTC

  • Multiple operating systems
    • All should use same standard
      • conflict when turning off
    • when using localtime more than one operating system may adjust it after a DST
    • Moving between timezones and using one of the operating systems to reset the system/hardware clock.

NTP (network time protocol)

Network Time Protocol is a networking protocol for clock synchronization between computer systems

In [17]:
grep -i ntp /etc/systemd/timesyncd.conf
#NTP=
#FallbackNTP=ntp.ubuntu.com
In [ ]:
NTP=ir.pool.ntp.org
In [23]:
systemctl is-enabled systemd-timesyncd.service
disabled

In [1]:
sudo timedatectl set-ntp true
In [10]:
systemctl is-enabled systemd-timesyncd.service
enabled
In [11]:
timedatectl status | grep -i ntp
NTP synchronized: yes

Timezone

tzdata

In [ ]:
sudo dpkg-reconfigure tzdata

<img src="img/dpkg-rec-tzdata.png" /img>

In [1]:
cat /etc/timezone
Asia/Tehran
In [2]:
file /etc/localtime
/etc/localtime: symbolic link to /usr/share/zoneinfo/Asia/Tehran

We will get back to these files for manual configuration.

timedatectl

In [10]:
timedatectl list-timezones | grep -i tehran
Asia/Tehran
In [11]:
sudo timedatectl set-timezone Asia/Tehran
In [13]:
timedatectl status | grep zone
       Time zone: Asia/Tehran (IRST, +0330)
In [12]:
timedatectl status | grep zone # In DST
       Time zone: Asia/Tehran (IRDT, +0430)

tzselect

It comes handy when you want to know what time it is in other countries, or if you just wonder what timezones exist.

In [ ]:
tzselect

<img src="img/tzselect.png" /img>

  • You can make this change permanent for yourself by appending the line:
    • TZ='Asia/Tehran'; export TZ to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you can use the /usr/bin/tzselect command in shell scripts:
Asia/Tehran

In [ ]:
tzselect 2> /dev/null
In [ ]:
4
16
1
Asia/Tehran
In [8]:
export TZ=Europe/Germany
In [9]:
date
Wed Mar 22 12:59:35 Europe 2017
In [10]:
export TZ=Asia/Tehran
In [11]:
date
Wed Mar 22 17:29:38 IRDT 2017

Config files

In [6]:
cat /etc/timezone
Asia/Tehran
In [11]:
ll /etc/localtime
lrwxrwxrwx 1 root root 31 Mar 19 22:11 /etc/localtime -> /usr/share/zoneinfo/Asia/Tehran
In [4]:
ls /usr/share/zoneinfo/
Africa      Cuba     GMT0         Japan              Pacific     Turkey
America     EET      GMT-0        Kwajalein          Poland      UCT
Antarctica  Egypt    GMT+0        leap-seconds.list  Portugal    Universal
Arctic      Eire     Greenwich    Libya              posix       US
Asia        EST      Hongkong     localtime          posixrules  UTC
Atlantic    EST5EDT  HST          MET                PRC         WET
Australia   Etc      Iceland      Mexico             PST8PDT     W-SU
Brazil      Europe   Indian       MST                right       zone1970.tab
Canada      Factory  Iran         MST7MDT            ROC         zone.tab
CET         GB       iso3166.tab  Navajo             ROK         Zulu
Chile       GB-Eire  Israel       NZ                 Singapore
CST6CDT     GMT      Jamaica      NZ-CHAT            SystemV
In [7]:
ls -l /usr/share/zoneinfo/Asia/Tehran
lrwxrwxrwx 1 root root 7 Dec  7 14:29 /usr/share/zoneinfo/Asia/Tehran -> ../Iran
In [5]:
file /usr/share/zoneinfo/Iran
/usr/share/zoneinfo/Iran: timezone data, version 2, 7 gmt time flags, 7 std time flags, no leap seconds, 101 transition times, 7 abbreviation chars

Manually

In [ ]:
echo Asia/Tehran | sudo tee /etc/timezone
In [ ]:
sudo ln -l /usr/share/zoneinfo/Asia/Tehran /etc/localtime

locale

locale is a set of parameters that defines the user's language, region and any special variant preferences that the user wants to see in their user interface. Usually a locale identifier consists of at least a language identifier and a region identifier.

In [1]:
locale # out config
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
In [2]:
timedatectl | head -1
      Local time: Sun 2017-03-19 22:54:37 IRST
In [3]:
locale -a | grep -i fa # show all available locales
fa_IR
fa_IR.utf8
In [4]:
export LC_TIME=fa_IR.utf8
In [5]:
timedatectl | head -1
      Local time: یکشنبه 2017-03-19 22:54:46 IRST

Managing locales

lets see what we got

In [9]:
grep '^[a-z]' /etc/locale.gen
en_US.UTF-8 UTF-8
fa_IR UTF-8

lets see if we got any locale enabled for france:

In [1]:
localectl list-locales | grep -i fr # eq to locale -a

In [3]:
locale -a | grep -i fr

Generate a new locale

In [ ]:
sudo nano /etc/locale.gen # uncomment # fr_FR.UTF-8 UTF-8

lets check the file again

In [3]:
grep -v '^#' /etc/locale.gen | grep -v '^$'
en_US.UTF-8 UTF-8
fa_IR UTF-8
fr_FR.UTF-8 UTF-8
In [4]:
whatis locale-gen
locale-gen (8)       - generates localisation files from templates
In [ ]:
sudo locale-gen
In [4]:
localectl list-locales | grep -i fr
fr_FR.utf8
In [5]:
locale -a | grep -i fr
fr_FR.utf8
In [6]:
export LANG=fr_FR.utf8
In [25]:
timedatectl | head -1
      Local time: lun. 2017-03-20 01:55:59 IRST

Make changes permanent

In [ ]:
localectl set-locale LANG=fr_FR.utf8

Also like always we can use .profile or anything like that to export LANG or other related environment variables.



Lecture notes

License

Creative Commons License

Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


ravexina's gitlab

ravexina's github